package com.example.firstjavamqtt.service.schedule;

import com.example.firstjavamqtt.Constants.DeviceTypeConstants;
import com.example.firstjavamqtt.model.base.Device;
import com.example.firstjavamqtt.service.base.DeviceService;
import com.example.firstjavamqtt.service.log.DeviceLogService;
import com.example.firstjavamqtt.service.log.InfraredLogService;
import com.example.firstjavamqtt.utils.RedisUtil;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Data
public class SpringScheduledTaskService {

    private Map<String, Date> map;

    private boolean isSetOffline = false;

    private DeviceService deviceService;

    private DeviceLogService deviceLogService;

    private InfraredLogService infraredLogService;

    private RedisUtil redisUtil;

    //心跳定时询问
    @Scheduled(cron = "0/5 * * * * *")
    public void isOnline() {
        //没任何设备在线的时候
        if (map == null) {
            if (!isSetOffline) {
                isSetOffline = true;
                deviceService.setAllDeviceOffline();
                return;
            }
            return;
        }
        map.forEach((a, lastTime) -> {
            deviceService.setDeviceStatus(a, new Date().getTime() - lastTime.getTime() <= 5000);
        });
        isSetOffline = false;
    }

    //每隔4小时 持久化一次数据库
    @Scheduled(cron = "0 0 0/4 * * *" )
    public void saveInfraredLog() {
        //lambda表达式 先根据deviceType进行过滤，然后再根据deviceID变成集合
        List<String> collect = deviceService.getDeviceList().stream()
                .filter(device -> DeviceTypeConstants.INFRARED.equals(device.getDeviceType()))
                .map(Device::getDeviceId)
                .collect(Collectors.toList());
        for (String deviceId : collect) {
            //根据deviceId查询redis
            //存到数据库中
            infraredLogService.saveByDeviceId(deviceId);
            deviceLogService.saveInfraredByDeviceId(deviceId);
            List<Object> objects = redisUtil.lGet("Infrared:" + deviceId, 0, -1);
            redisUtil.del("Infrared:" + deviceId);
            //removeInfraredRedis(deviceId, objects);
        }
        System.out.println("定时任务持久化有人走过完成");
    }

    ////移除Redis中的对应数据
    //private void removeInfraredRedis(String deviceId, List<Object> objects) {
    //    if(CollectionUtils.isEmpty(objects)){
    //        return;
    //    }
    //    for (Object object : objects) {
    //        redisUtil.lRemove("Infrared:" + deviceId,1,object);
    //    }
    //}

    @Autowired
    public void setDeviceService(DeviceService deviceService) {
        this.deviceService = deviceService;
    }

    @Autowired
    public void setDeviceLogService(DeviceLogService devicelogService) {
        this.deviceLogService = devicelogService;
    }

    @Autowired
    public void setRedisUtil(RedisUtil redisUtil) {
        this.redisUtil = redisUtil;
    }

    @Autowired
    public void setInfraredLogService(InfraredLogService infraredLogService) {
        this.infraredLogService = infraredLogService;
    }
}
